﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XAPI.Domain;
using XAPI.Data;
using System.Data.Entity.Validation;

namespace TinCan.LRS.Web.Controllers
{
    public class ManageActivitiesController : Controller
    {
        private EFContext db = new EFContext();

        //
        // GET: /ManageActivities/

        public ActionResult Index()
        {
            return View(db.Activities.ToList());
        }

        //
        // GET: /ManageActivities/Details/5

        public ActionResult Details(string id = null)
        {
            Activity activity = db.Activities.Find(id);
            if (activity == null)
            {
                return HttpNotFound();
            }
            return View(activity);
        }

        //
        // GET: /ManageActivities/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /ManageActivities/Create

        [HttpPost]
        public ActionResult Create(Activity activity)
        {
            var map = new LanguageMap();
            map.Add("test", "testvalue");

            if (ModelState.IsValid)
            {
                activity.definition = new ActivityDefinition()
                {
                    description  = map
                };

                db.Activities.Add(activity);
                try
                {
                    db.SaveChanges();
                }
                catch (DbEntityValidationException ex)
                {
                    foreach (var item in ex.EntityValidationErrors)
                    {
                        Console.WriteLine(item.Entry.Entity.ToString());
                        Console.WriteLine(item.IsValid);
                        foreach (var error in item.ValidationErrors)
	                    {
                            Console.WriteLine(error.PropertyName + ": " + error.ErrorMessage);
	                    }
                    }
                }
                
                return RedirectToAction("Index");
            }

            return View(activity);
        }

        //
        // GET: /ManageActivities/Edit/5

        public ActionResult Edit(string id = null)
        {
            Activity activity = db.Activities.Find(id);
            if (activity == null)
            {
                return HttpNotFound();
            }
            return View(activity);
        }

        //
        // POST: /ManageActivities/Edit/5

        [HttpPost]
        public ActionResult Edit(Activity activity)
        {
            if (ModelState.IsValid)
            {
                db.Entry(activity).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(activity);
        }

        //
        // GET: /ManageActivities/Delete/5

        public ActionResult Delete(string id = null)
        {
            Activity activity = db.Activities.Find(id);
            if (activity == null)
            {
                return HttpNotFound();
            }
            return View(activity);
        }

        //
        // POST: /ManageActivities/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {
            Activity activity = db.Activities.Find(id);
            db.Activities.Remove(activity);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}